package com.czd.support.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.support.model.Manager;
import com.czd.support.model.Site;
import com.czd.support.model.SystemConfig;
import com.czd.support.service.SiteService;
import com.czd.support.service.SystemConfigService;

@Controller
@RequestMapping(value = "/sysset")
public class SystemConfigController extends BaseController {
	@Autowired
	private SystemConfigService systemConfigService;
	@Autowired
	private SiteService siteService;
	
	@ModelAttribute("initSiteList")
	private List<Site> initSiteList() {
		return siteService.findSiteList();
	}
	
	@RequestMapping(value = "/{id}/details",method = RequestMethod.GET)
    public String view(@PathVariable int id,Model model){
		SystemConfig systemConfig = systemConfigService.findById(id);
        if(systemConfig != null){
            model.addAttribute("systemConfig",systemConfig);
            return "sysset/details";
        }
        return error(model,"信息不存在","信息内容不存在");
    }
	
	/**
	 * 删除操作
	 * @param id
	 * @param systemConfig
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/delete",method = RequestMethod.GET)
	public String delete(@PathVariable int id,SystemConfig systemConfig,Model model){
		System.out.println(systemConfig);
		if (systemConfigService.delete(systemConfig)) {
			return "redirect:/sysset/list";
		} else {
			return error(model, "操作失败", "删除的信息内容已经不存在");
		}
	}
	
	/**
	 * 修改记录
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/edit",method = RequestMethod.GET)
    public String change(@PathVariable int id,Model model){
		SystemConfig systemConfig = systemConfigService.findById(id);
        if(systemConfig != null){
            model.addAttribute("systemConfig",systemConfig);
            return "sysset/edit";
        }
        return error(model,"信息不存在","内容不存在");
    }
	
	@RequestMapping(value = "/{id}/edit",method = RequestMethod.POST)
    public String update(@PathVariable int id,SystemConfig systemConfig,HttpServletRequest request,Model model){
	//	Manager manager = this.getSesionUser(request);
		System.out.println(systemConfig);
		if (systemConfigService.update(systemConfig)) {
			return "redirect:/sysset/list";
		} else {
			return error(model, "操作失败", "信息内容已经不存在");
		}
	
	   }
	
	
	/**
	 * 查询列表
	 * @param model
	 * @param systemConfig
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value = {"","/","list"},method = RequestMethod.GET)
    public String list(Model model,SystemConfig systemConfig,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
		systemConfig.setPageNo(pageNo);
        model.addAttribute("paginate",systemConfigService.queryForPaginate(systemConfig));
        return "sysset/list";
    }
	
	/**
	 * 录入系统信息内容
	 * @param systemConfig
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addcontent" , method = RequestMethod.POST)
	public String save(SystemConfig systemConfig,HttpServletRequest request,Model model){
		Manager manager = this.getSesionUser(request);
		systemConfig.setCreateUser(manager.getUserCode());
		systemConfig.setUpdateUser(manager.getUserCode());
		if(StringUtils.isBlank(systemConfig.getCategory())){
			return  error(model, "操作提示", "信息类别不许为空");
		}
		if(StringUtils.isBlank(systemConfig.getContent())){
			return  error(model, "操作提示", "内容不许为空");
		}
		if(systemConfigService.insert(systemConfig)){
			return "redirect:/sysset/list";
		}
		return  error(model, "操作提示", "系统设置内容录入失败");
	}
	
	@RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "sysset/add";
    }
	
	@RequestMapping(value = "/add",method = RequestMethod.POST)
    public String save() {
       
        return "sysset/add";
    }

}
